-- title:  palette demo
-- author: Nesbox
-- desc:   how to switch palatte in runtime
-- script: lua
-- input:  gamepad

local W=240
local H=136

-- palette address
local ADDR=0x3FC0

local PALETTES =
{
	{name="SWEETIE-16", data="1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7333c57566c8694b0c2f4f4f4"},
	{name="DB16",		     data="140c1c44243430346d4e4a4e854c30346524d04648757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6"},
	{name="PICO-8",     data="0000007e25531d2b535f574fab5236008751ff004d83769cff77a8ffa300c2c3c700e756ffccaa29adfffff024fff1e8"},
	{name="ARNE16",     data="0000001b2632005784493c2ba4642244891abe26332f484e31a2f2eb89319d9d9da3ce27e06f8bb2dceff7e26bffffff"},
	{name="EDG16",	     data="193d3f3f2832743f399e2835b86f50327345e53b444f67810484d1fb922bafbfd263c64de4a6722ce8f4ffe762ffffff"},
	{name="A64",			     data="0000004c3435313a9148545492562b509450b148638080787655a28385cf9cabb19ccc47cd93738fbfd5bbc840ede6c8"},
	{name="C64",			     data="00000057420040318d5050508b542955a0498839327878788b3f967869c49f9f9f94e089b8696267b6bdbfce72ffffff"},
	{name="VIC20",      data="000000772d2642348ba85fb4b668627e70caa8734a559e4ae99df5e9b287bdcc7185d4dc92df87c5ffffffffb0ffffff"},
	{name="CGA",        data="000000aa00000000aa555555aa550000aa00ff5555aaaaaa5555ffaa00aa00aaaa55ff55ff55ff55ffffffff55ffffff"},
	{name="SLIFE",      data="0000001226153f28117a2222513155d13b27286fb85d853acc8218e07f8a9b8bff68c127c7b581b3e868a8e4d4ffffff"},
	{name="JMP",        data="000000191028833129453e78216c4bdc534b7664fed365c846af45e18d79afaab9d6b97b9ec2e8a1d685e9d8a1f5f4eb"},
	{name="CGARNE",     data="0000002234d15c2e788a36225e606e0c7e45e23d69aa5c3d4c81fb44aacceb8a60b5b5b56cd9477be2f9ffd93fffffff"},
	{name="PSYG",       data="0000001b1e29003308362747084a3c443f41a2324e52524c546a0073615064647c516cbf77785be08b799ea4a7cbe8f7"},
	{name="EROGE",      data="0d080d2a23494f2b247d384032535f825b314180a0c16c5bc591547bb24e74adbbe89973bebbb2f0bd77fbdf9bfff9e4"},
	{name="EISLAND",    data="051625794765686086567864ca657e8686918184abcc8d867ea78839d4b98dbcd29dc085edc38de6d1d1f5e17af6f6bf"},
}


local t=0
local index=1

-- update palette
function updpal()
	local pal=PALETTES[index].data
	for i=1,#pal,2 do
		poke(ADDR+i//6*3+i//2%3,tonumber(pal:sub(i,i+1),16))
	end	
end

updpal()

function TIC()
	
	-- handle input
	if btnp(0,20,5) and index>1 then 
		index=index-1 
		updpal()
	end	
	if btnp(1,20,5) and index<#PALETTES then 
		index=index+1 
		updpal()
	end
	
	if btnp(4)or btnp(5)then exit()end
	
	-- draw
	
	cls(15)
	
	print("SELECT PALETTE",6,6,0)
	
	for i,v in pairs(PALETTES) do
		print(v.name,12,12+i*6,0)	
	end
	
	print(">",6+(t//16%2),12+index*6,0)
	
	local S=12
	
	-- draw palette
	for i=0,16-1 do
		for j=0,S-1 do
			line(W-j-i*S,H,W,H-j-i*S,i)
		end
	end
	
	t=t+1
end

-- <PALETTE>
-- 000:140c1c44243430346d4e4a4e854c30346524d04648757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6
-- </PALETTE>

-- <COVER>
-- 000:c99000007494648373160f0088003b000041c0c14442430343d6e4a4e458c4034356420d648457171695d7ec2dd7c258591ad6aac22daa99d62cacad4de5edee6dc2000000000f0088000040ff0f9c94badb83bedcbbff0682e87936e98a9dcac6bb2307c23cb4d6f246eafec7feff89490d1ea26366846cde60c6a3f90d82768923ab293b5235acdea7fbd1a2acad2cac4bb50ba7dce6a8f446699368e47bfe8fcf6edaa8f17ea4576371005800f0685888783198c8682198512919a8b841e8b8a82999a7e9b7c7e7f7372863a061095989c8aad8f8daba1979883b0b0b6969f9ab141ae708185aa07a8a8b9a9ba9f8ca6bda788b5b8cea1d0dbb4d21c744eb085a532c3c4cdcac7c2efafc595c093e39ca5c5dde7d2afbbdb0ddedea7eb9c9499a5efcec99d99bee92b6787ed56325bd8e5d328c0b1a7038172e52458e0b2a54e5f60e96b9841f2a74b801ff9c84c06a058d7c39c034a591928a46734903779a432c2be8bc79133761ce5117329cb9b057a0de233b84d4430417e4a557f4a96a1a057241db2e3b5625509003cab75ef8d54d8a35bc455b97531465dacc6adc2a4f5376859336196953b863da3b5b4ead2ac7bf22ea69ba85dce9d5a6f2953d7aa77f2f419e7790061c4731b4611c77499197f812f0ec0afc850c366f62bcc5cb2b1fd8cca993cd5fa0e465c2a52a5f45e3af77ea6edca59c975bb7e26bc62d43d4b92bd7b55fe36db65d9b6fdc666b07e64cb8f6817dbdc1120dccfb83748f318ede4d9d99f6fbcc1dbf778cd1c332708b570687823e1b06e8efd1181795d5b95fbddfe5a4cf423f5257e1ff46808d73d175802a37c916ee5f7f3cf7844086ed089041860a60ff876790868c938e600764d373f0d77b95871276802e7902e35d978f028845177659084165862ab80514895497735d863ed870a28f9de6bf0e8048e8760f8e29f862a587f943da586cf819d30294782952139d46186cda715e164565903a852329543258b5a3970eb6f5233975899385943069a096996e989b909de183d22c9428c90ccc831e0877aa9e7a3408a48d36d858a1a81e09fad1a74e890759927d994863a80c10f464925a4ae342a472d36761a7007ab62d2d91c60a2f02a677b82a9a9e9a8ae3aa0aa8546ba32cba286ca9963a4b6da82ab69b638abe70cba9938e97a9e6a1ceff1be942b79e2b5fdca1ca0b826b71924b8c25ad795a5b16acca5b7d67a5e4eacd28291d144e1cab423a4e69b9aa939c0ba9eaab268640ea3642a66dce5bfc6aad66727f6602d68b4d6981f20c9e14a8941ca1c1ccfa8b5d20b0070c01bf7eda562fd8add49a77e5c2fe3a8ba164fe7aaba4c7a5fb40e2c71b106e2e5bcd68f6a4aa2f0741b2c61b8c7aa1003b0ce6aa35fadb78edc0d5ecefabc00bfcf3709056a8056ba64f88847bcb33fcc473d3d5166d1236238a353a6fba9ac37fc9576cf1eabd3debb275d28dfb56f1095bfcd0fe9d571c163bcf6b2d7decd4730d9e01d7c6ede6b950779de7ffd427438fccc38f6c4bd4c9072d75b8c98b2e3caaab2aff1b5434e22a8d3e65ef2f5ea576bc5fd5f8badc81bd83f4e6539d8839e5a3ad1201eeafed2af2d2bfce7d53e7a7e38e6876bbfa1a3ce85fee50479e450dbcb783cfbb2b6eed732f9cb2f7e23872b3fe93be74fde683fd1ceee604104eb707b2b91e07eebd3f952cb2d78f4d79f14238c7a7ae57f7ce7d9eeb1f697cf2f3de168de5bba750db1b58c2e62e32f920ba76ba6c1b84d78f31067ccd5deb8e1ff6472bb50a20f30f0c635892f74aadfd10367914d0effee39127c96f4575a1ee15dcb7f3ab0aeea4862460e0b631aa25b0e8048f13116a92e6273ff990be6bc1bfde0528002f8903046f3440a0178800c21021324a4c0c8507487c3a16d0f486abceff9a76865f380e54878b44f1e6908890cd2a209006ea0cd0c0788d17bc41774414dfd51b984242d9a17d8c6c971dc2939f970e058c8654302e13b8a7cd3610800c8c63e6e6a1e04132846e894aa3680fe876472a617b8c8c64a5f0a1d515159c4294012423133997cbc922719d9ce3200d4966c621a1f9862c65a72b18fb38d0b54f20b4c4ac253998cd3ad2f69993e313e6a4f59e5a1215988455a227894cccd01ba92ebb6b823b8785cefd62b680dc866fe2d706ca9d02929752b46f0d7962a95e63b69dd47566e2b75b8d5e839581c17128ea2623c9eda3538de4f66ae2f5cea90210b28bccf56a2317bace7af358628cbb10f2e8519b62e33b981c06aa15ff1a40810c398732ac6ed7442aa3bc72cf6f9b04c8e0043a3f3e8a6e86601d68084927225a55a1589a0d392a4717e548a9117c84ec93993bc88e4b09a3d2ac9ca9ad4bb4ebbfa1825e9e2de960a4954cb98e125b9445e6a15f8a9237aa2aa088dc9a654d9943de7ad41bad0cafd9c834e5a69ea1e5afa3a8ec0baace41be85f4ab1573d658d4f41f06e18da8bb89a9d84ac749bec5f6a9dabb67dcd87f34ddd18fafdb7f9c4969cfcbae06beabf1bf5a039ab75da651f4960d66224f9929d9cecfccad993fdb15da441dcef4b09d55662551ca71d0da86f38aad18ad1b0a9e4062c6b8a28d1c295d84c8d81ab55f907d8c6f6d7b300100d69e9122b7aeda1be7ffd022069c76254ead4500ac50e6d61aac559aab6fbaf9d0b6b3dab7d5cea6799b13a1027790adadeceb63cb1bd3eaf61bb6e571507f07e95fb52456b385af6d7d147f50fd367abca49f6f7dfb9dd802a79c4aed9654534a3cd19617b4acfd9b228ffb2cbdea38bbb016faeb577b65530b58f2c90ece408d1bf7d907067cb4ed4e298b4cbe3d032ce1c03d80f0855c4dda0f85f5c81e8dc983ab33d8f2b89cbc3ee1f46e7c63e413883ea346be619d8c98142b7898bb463e63919bf461040026cb0ea453876cc1ea1f4952c556a0b8877cf4e00c6954325efee781ebcbdb2fc73dca6663bc852743e6be89f6c56e51773b5ce2e22ba9bec29963bb71cbaf50deff99bcb28e14fa997977e6b1c01fbfed44b87ffcd2ec2b3a90d6dc847be2fcd7d42b4717c50693f18b1db9684779fb5f56c37895fce66a23c934c6ae113aa35d9c4f477f04d0d5157093ec896223d973dc9e972047d6da6943fa58bb7652fca99c1c672f0b98df861a81a37b705b4b093ad85e9635b374abe71e2b92d88e9bde771d725d435bd1d6c6a28eb29df9e7578ab9d3aee37e81dd186e1fabd1d4ac4948bfe9d1e85399f9d656e6fc9f6dbe64d22aefdfc566b0c9fc407337da1dc807a670f87aba6e852175bc95c67e7dcda8e9833a5fdbf65333513e7ce5d1cb77d2fee53dbf7d4b638f9a94e52728d7491e58378f1afdcad60634310daeea9bdc1515eeee08e6cd6b794cda0557b117f3ffc33ded6b8b32afdf98b6f2d3bd02fb4f3d58d51726f4d51e3a9c874e49dd27d9b7cb2d95fd6f6d5add5fd4f7d1ce557c9f4a50cecee97cc7dedde6b3fbfde008800000b3
-- </COVER>

